package com.zds.support.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.zds.support.R
import com.zds.support.extensions.setWidthAndHeight
import com.zds.support.util.NetUtils

/**
 *
 *@Description
 *@Author zc
 *@Date 2021/1/22 0022 14:33
 *@Version V1.0
 **/
abstract class BaseRecyclerAdapter<VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<RecyclerView.ViewHolder> {

    companion object {
        const val S_TYPE_EMPTY = -25555411

        const val EMPTY_NOTICE_STATUS_LOADING = 1
        const val EMPTY_NOTICE_STATUS_NO_DATA = 2

    }

    protected val mContext: Context
    protected var mIsShowEmptyView = true
    protected var mLoadingMsg: String = ""
    protected var mLoadingImageViewResId: Int = R.drawable.support_ic_data_empty
    protected var mEmptyMsg: String = ""
    protected var mEmptyImageViewResId: Int = R.drawable.support_ic_data_empty
    protected var mEmptyStatus: Int = EMPTY_NOTICE_STATUS_NO_DATA
    protected var mEmptyImageViewHeight = 0
    protected val mLayoutInflater by lazy {
        getLayoutInflater()
    }

    private fun getLayoutInflater(): LayoutInflater {
        return LayoutInflater.from(mContext)
    }

    constructor(context: Context) : this(context, true)

    constructor(context: Context, showEmptyView: Boolean) : super() {
        this.mContext = context
        this.mIsShowEmptyView = showEmptyView
        this.mLoadingMsg = "加载失败"
        this.mEmptyMsg = "无数据"
        setHasStableIds(true)
    }


    constructor(context: Context, @DrawableRes mEmptyImageViewResId: Int, @StringRes emptyMsgRes: Int)
            : super() {
        this.mContext = context
        this.mEmptyImageViewResId = mEmptyImageViewResId
        this.mEmptyMsg = context.getString(emptyMsgRes)
        this.mLoadingMsg = "加载失败"
        setHasStableIds(true)
    }

    constructor(context: Context, @DrawableRes mEmptyImageViewResId: Int, emptyMsg: String)
            : super() {
        this.mContext = context
        this.mEmptyImageViewResId = mEmptyImageViewResId
        this.mEmptyMsg = emptyMsg
        setHasStableIds(true)
        this.mLoadingMsg = "加载失败"
    }

    constructor(context: Context, @StringRes emptyMsgRes: Int) : super() {
        this.mContext = context
        this.mEmptyMsg = context.getString(emptyMsgRes)
        this.mLoadingMsg = "加载失败"
        setHasStableIds(true)
    }

    open fun updateEmptyNoticeStatus(noticeStatus: Int) {
        mEmptyStatus = noticeStatus
        if (getAdapterItemCount() == 0 && mIsShowEmptyView) {
            notifyDataSetChanged()
        }
    }

    open fun setEmptyImageViewHeight(height: Int) {
        this.mEmptyImageViewHeight = height
    }

    override fun getItemId(position: Int): Long {
        return position * 1L
    }

    open fun updateEmptyViewData(@DrawableRes emptyImageViewResId: Int, @StringRes emptyMsgRes: Int) {
        updateEmptyViewData(emptyImageViewResId, mContext.getString(emptyMsgRes))
    }

    fun notifyDataSetChanged(isShowEmptyView: Boolean) {
        this.mIsShowEmptyView = isShowEmptyView
        notifyDataSetChanged()
    }

    fun setShowEmptyView(isShowEmptyView: Boolean) {
        this.mIsShowEmptyView = isShowEmptyView
        notifyDataSetChanged()
    }

    open fun updateEmptyViewData(@DrawableRes emptyImageViewResId: Int, emptyMsg: String) {
        this.mEmptyMsg = emptyMsg
        this.mEmptyImageViewResId = emptyImageViewResId
        if (getAdapterItemCount() == 0 && mIsShowEmptyView) {
            notifyDataSetChanged()
        }
    }

    open fun updateEmptyMsg(emptyMsg: String) {
        this.mEmptyMsg = emptyMsg
        if (getAdapterItemCount() == 0 && mIsShowEmptyView) {
            notifyDataSetChanged()
        }
    }

    open fun notifyDataSetChangedAndUpdateEmptyNoticeStatus(noticeStatus: Int = EMPTY_NOTICE_STATUS_NO_DATA) {
        mEmptyStatus = noticeStatus
        super.notifyDataSetChanged()
    }

    final override fun getItemCount(): Int {
        if (getAdapterItemCount() == 0 && mIsShowEmptyView) {
            return 1
        } else {
            return getAdapterItemCount()
        }
    }

    final override fun getItemViewType(position: Int): Int {
        if (getAdapterItemCount() == 0 && mIsShowEmptyView) {
            return S_TYPE_EMPTY
        } else {
            return getAdapterItemViewType(position)
        }
    }


    final override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (isEmptyViewHolder(holder)) {
            onBindEmptyViewHolder(holder)
        } else {
            try {
                onBindAdapterViewHolder(holder as VH, position)
            } catch (e: Exception) {
            }

        }
    }

    final override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (S_TYPE_EMPTY == viewType) {
            return getEmptyViewHolder(parent)
        } else {
            return onCreateAdapterViewHolder(parent, viewType)
        }
    }


    open fun getEmptyViewHolder(parent: ViewGroup?): RecyclerView.ViewHolder {
        return EmptyViewHolder(mContext, parent, getEmptyViewHolderLayoutId())
    }

    fun onBindEmptyViewHolder(holder: RecyclerView.ViewHolder) {
        if (holder is EmptyViewHolder) {
            try {
                val emptyViewHolder: EmptyViewHolder = holder
                if (mEmptyStatus == EMPTY_NOTICE_STATUS_LOADING) {
                    emptyViewHolder.setData(mLoadingImageViewResId, mLoadingMsg, mEmptyStatus, mEmptyImageViewHeight)
                } else {
                    emptyViewHolder.setData(mEmptyImageViewResId, mEmptyMsg, mEmptyStatus, mEmptyImageViewHeight)
                }
            } catch (e: Exception) {

            }


        }

    }

    open fun getEmptyViewHolderLayoutId(): Int {
        return R.layout.support_item_data_empty
    }

    /**
     * 判断是否是EmptyViewHolder
     */
    fun isEmptyViewHolder(holder: RecyclerView.ViewHolder): Boolean {
        return holder is EmptyViewHolder
    }

    open fun getEmptyViewHolderSpanSizeLookup(manager: GridLayoutManager): GridLayoutManager.SpanSizeLookup {
        return object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                val type = getItemViewType(position)
                val spanCount = manager.spanCount
                if (type == S_TYPE_EMPTY) {
                    return spanCount
                } else {
                    return getGridLayoutManagerSpanSize(position)
                }
            }
        }
    }

    open class EmptyViewHolder : BaseViewHolder {
        private val context: Context

        constructor(context: Context, parent: ViewGroup?, layoutId: Int) : super(context, parent, layoutId) {
            this.context = context
        }

        open fun setData(emptyDrawableRes: Int, emptyMsg: String, emptyStatus: Int, imageHeight: Int) {
            with(itemView) {
                try {
                    if (emptyStatus == EMPTY_NOTICE_STATUS_LOADING) {
                        // findViewById<View?>(R.id.supportPbLoading)?.visibility = View.VISIBLE
                        //findViewById<View?>(R.id.supportIvEmpty)?.visibility = View.GONE
                    } else {
                        // findViewById<View?>(R.id.supportPbLoading)?.visibility = View.GONE
                        //findViewById<View?>(R.id.supportIvEmpty)?.visibility = View.VISIBLE
                    }
                    val isNetConnected = NetUtils.isNetConnected()
                    val supportIvEmpty = findViewById<View?>(R.id.supportIvEmpty)
                    if (supportIvEmpty != null && supportIvEmpty is ImageView) {
                        if (isNetConnected) {
                            supportIvEmpty.setImageResource(emptyDrawableRes)
                        } else {
                            supportIvEmpty.setImageResource(R.drawable.support_ic_data_empty)
                        }

                        if (imageHeight > 0) {
                            supportIvEmpty.setWidthAndHeight(imageHeight, imageHeight)
                        }
                    }
                    val supportTvEmptyMsg = findViewById<View?>(R.id.supportTvEmptyMsg)
                    if (supportTvEmptyMsg != null && supportTvEmptyMsg is TextView) {
                        if (isNetConnected) {
                            supportTvEmptyMsg.text = emptyMsg
                        } else {
                            supportTvEmptyMsg.setText("网络错误")
                        }

                    }
                } catch (e: Exception) {

                }

                //  supportTvEmptyMsg.text = emptyMsg
            }

            //  supportIvEmptyNotice.setImageResource(emptyIconRes)

        }


    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        val manager = recyclerView.layoutManager
        if (manager is GridLayoutManager) {
            manager.spanSizeLookup = getEmptyViewHolderSpanSizeLookup(manager)

        }
        super.onAttachedToRecyclerView(recyclerView)
    }

    override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
        val lp = holder.itemView.layoutParams
        if (lp != null && lp is StaggeredGridLayoutManager.LayoutParams) {
            if (holder is EmptyViewHolder) {
                lp.isFullSpan = true
            }
        }
        super.onViewAttachedToWindow(holder)
    }

    open fun getGridLayoutManagerSpanSize(position: Int): Int = 1

    open fun getAdapterItemViewType(position: Int): Int = 0

    abstract fun getAdapterItemCount(): Int

    abstract fun onCreateAdapterViewHolder(parent: ViewGroup?, viewType: Int): VH
    abstract fun onBindAdapterViewHolder(holder: VH, position: Int)
}